<?php

namespace oa\validates;

use Yii;

class UserEditValidate extends \yii\base\Model
{
	public $password;
	public $new_password;
    public $re_password;


    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['password', 'new_password', 're_password'], 'required'],
            [['password', 'new_password', 're_password'], 'string', 'min' => 3, 'max' => 32],
            ['re_password', 'compare', 'compareAttribute' => 'new_password', 'message'=>'两次输入的密码不一致！'],
            ['password', 'validatePassword'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'password' => '账号密码',
            'new_password' => '新密码',
            're_password' => '重复新密码',
        ];
    }

    public function validatePassword($attribute, $params)
    {
        if (!$this->hasErrors()) {
            if (!Yii::$app->security->validatePassword($this->password, Yii::$app->user->identity->password)) {
                $this->addError($attribute, '账号密码不正确');
            } elseif ($this->password == $this->new_password) {
                $this->addError($attribute, '账号密码和新密码一致');
            }
        }
    }

}
